package com.example.valuation.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.example.valuation.R;
import com.example.valuation.adapterr.ZhiShuAdapter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * KG
 */
public class Tab1Fragment extends Fragment {
    @BindView(R.id.et_price)
    EditText et_price;
    @BindView(R.id.tv_tariff)
    EditText tv_tariff;
    @BindView(R.id.et_zzs)
    EditText et_zzs;
    @BindView(R.id.et_huilv)
    EditText et_huilv;
    @BindView(R.id.et_cost)
    EditText et_cost;
    @BindView(R.id.et_hsdj)
    EditText et_hsdj;
    @BindView(R.id.et_tax_point)
    EditText et_tax_point;
    @BindView(R.id.et_bhsdj)
    EditText et_bhsdj;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.et_jishu)
    EditText et_jishu;
    @BindView(R.id.et_pinpai)
    EditText et_pinpai;
    @BindView(R.id.et_zhishu)
    EditText et_zhishu;
    private ZhiShuAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab1, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new ZhiShuAdapter(R.layout.item_zhishu);
        recyclerView.setAdapter(adapter);
    }

    private String getEditText(EditText editText) {
        return editText.getText().toString().trim();
    }

    private BigDecimal getDecimal(String value) {
        return new BigDecimal(value);
    }

    /**
     * 专门用于将百分比转成小数
     *
     * @param value 输入的百分比
     * @return
     */
    private BigDecimal getDecimal2(String value) {
        BigDecimal bigDecimal = new BigDecimal(value);
        return bigDecimal.divide(new BigDecimal("100"));
    }

    /**
     * 专门用于将百分比转成小数再加1
     *
     * @param value 输入的百分比
     * @return
     */
    private BigDecimal getDecimal3(String value) {
        BigDecimal bigDecimal = new BigDecimal(value);
        BigDecimal bigDecimal2 = bigDecimal.divide(new BigDecimal("100")).add(new BigDecimal("1"));
        return bigDecimal.divide(new BigDecimal("100")).add(new BigDecimal("1"));
    }

    @OnClick({R.id.btn_clear, R.id.btn_calculation})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.btn_clear:
                et_price.setText("");
                tv_tariff.setText("3.5");
                et_zzs.setText("13");
                et_huilv.setText("");
                et_cost.setText("350");
                et_hsdj.setText("");
                et_tax_point.setText("3.5");
                et_bhsdj.setText("");
                et_pinpai.setText("");
                et_zhishu.setText("");
                break;
            case R.id.btn_calculation:
                if (TextUtils.isEmpty(getEditText(et_price))) {
                    Toast.makeText(getContext(), "请输入单价", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(getEditText(tv_tariff))) {
                    Toast.makeText(getContext(), "请输入关税", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(getEditText(et_zzs))) {
                    Toast.makeText(getContext(), "请输入增值税", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(getEditText(et_huilv))) {
                    Toast.makeText(getContext(), "请输入汇率", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(getEditText(et_cost))) {
                    Toast.makeText(getContext(), "请输入费用", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(getEditText(et_tax_point))) {
                    Toast.makeText(getContext(), "请输入税点", Toast.LENGTH_SHORT).show();
                    return;
                }
                /**
                 * 单价*关税*增值税*汇率*1000+费用=含税单价
                 */
                BigDecimal bigDecimal = getDecimal(getEditText(et_price)).multiply(getDecimal3(getEditText(tv_tariff)))
                        .multiply(getDecimal3(getEditText(et_zzs))).multiply(getDecimal(getEditText(et_huilv)))
                        .multiply(getDecimal("1000")).add(getDecimal(getEditText(et_cost))).setScale(0, BigDecimal.ROUND_HALF_UP);
                et_hsdj.setText(bigDecimal.toString());
                /**
                 * 含税单价*（1-税点）=不含税单价
                 */
                BigDecimal bigDecimal2 = getDecimal("1").subtract(getDecimal2(getEditText(et_tax_point)));
                BigDecimal bigDecimal3 = getDecimal(getEditText(et_hsdj)).multiply(bigDecimal2);
                et_bhsdj.setText(bigDecimal3.setScale(0, BigDecimal.ROUND_HALF_UP).toString());
                //生成单价列表
                List<HashMap<String, String>> list = new ArrayList<>();
                List<Double> list1 = generateSortedNumbers(Double.parseDouble(getEditText(et_price)));
                for (double n : list1) {
                    /**
                     * 单价/181.44*关税*增值税*汇率*1000+费用=含税单价
                     */
                    BigDecimal bigDecimal4 = getDecimal(n + "").multiply(getDecimal3(getEditText(tv_tariff)))
                            .multiply(getDecimal3(getEditText(et_zzs))).multiply(getDecimal(getEditText(et_huilv)))
                            .multiply(getDecimal("1000")).add(getDecimal(getEditText(et_cost))).setScale(0, BigDecimal.ROUND_HALF_UP);
                    String text2 = bigDecimal4.toString();
                    /**
                     * 含税单价*（1-税点）=不含税单价
                     */
                    BigDecimal bigDecimal5 = getDecimal("1").subtract(getDecimal2(getEditText(et_tax_point)));
                    BigDecimal bigDecimal6 = getDecimal(text2).multiply(bigDecimal5);
                    String text3 = bigDecimal6.setScale(0, BigDecimal.ROUND_HALF_UP).toString();
                    HashMap<String, String> hashMap = new HashMap<>();
                    hashMap.put("text1", n + "");
                    hashMap.put("text2", text2);
                    hashMap.put("text3", text3);
                    list.add(hashMap);

                }
                adapter.setNewData(list);
                adapter.notifyDataSetChanged();
                break;
        }
    }

    /**
     * 生成以中心小数为基准的数字序列，并按从小到大排序
     *
     * @param center 中心小数
     * @return 包含中心数字和前后各5个数字的有序列表
     */
    public List<Double> generateSortedNumbers(double center) {
        //如果基数有值就取这个值，没有就默认为3
        double mNum = TextUtils.isEmpty(getEditText(et_jishu)) ? 0.02 : Double.parseDouble(getEditText(et_jishu));
        List<Double> numbers = new ArrayList<>();

        // 向前生成5个数字，间隔为mNum，递减
        for (int i = 1; i <= 5; i++) {
            double num = center - i * mNum;
            // 保留两位小数，避免浮点数精度问题
            numbers.add(Math.round(num * 100.0) / 100.0);
        }

        // 添加中心数字
        numbers.add(center);

        // 向后生成5个数字，间隔为mNum，递增
        for (int i = 1; i <= 5; i++) {
            numbers.add(center + i * mNum);
        }

        // 对所有数字进行排序（从小到大）
        Collections.sort(numbers);

        return numbers;
    }
}